package com.bolt.convention.data;


import org.springframework.data.domain.Sort;

/**
 * Created by Administrator on 2019/4/19.
 */
public class PageQuery implements Pagination {

    /**
     * 默认每页显示的记录数
     */
    public static final int DEFAULT_PAGE_SIZE = 20;


    /**
     * 默认每页显示的记录数
     */
    public static final int DEFAULT_PAGE_NUMBER = 1;

    private final int pageNumber;
    private final int pageSize;
    private final Sort sort;
    private int offset;



    public PageQuery(int pageNumber, int pageSize, Sort sort) {
        if (pageNumber < 1) {
            throw new IllegalArgumentException("PageInfo index must not be less than one!");
        }

        if (pageSize < 1) {
            throw new IllegalArgumentException("PageInfo size must not be less than one!");
        }

        this.pageNumber = pageNumber;
        this.pageSize = pageSize ;
        this.sort = sort;
    }

    public static PageQuery of(int page, int size) {
        return new PageQuery(page,size,Sort.unsorted());
    }

    public static PageQuery of(int page, int size, Sort sort) {
        return new PageQuery(page,size,sort);
    }



    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    public int getOffset() {
        return (pageNumber - 1) * pageSize;
    }

    public Sort getSort() {
        return sort;
    }



    @Override
    public String toString() {
        return "PageQuery{" +
                "pageNumber=" + pageNumber +
                ", pageSize=" + pageSize +
                '}';
    }

}

 